<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     * @throws \Exception
     */
    public function boot()
    {
        if (config('app.debug')) {
            \DB::enableQueryLog();
            $log = new Logger('sql');
            $log->pushHandler(
                (new StreamHandler(
                    storage_path('logs/sql.log'),
                    Logger::DEBUG
                ))->setFormatter(new LineFormatter(null, null, false, true))
            );
            \DB::listen(function ($query) use ($log) {
                $log->addDebug($query->time / 1000 . '|' . $query->sql);
            });
        }
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}
